package com.example.a7822.myapplication;

import android.os.Bundle;
import android.widget.Toast;

import org.linphone.CallOutgoingActivity;
import org.linphone.LinphoneManager;
import org.linphone.core.LinphoneCall;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneCoreListenerBase;
import org.linphone.core.Reason;

/**
 * Created by flyln on 2017/10/26 21:26.
 * email:xyf_uestc@163.com
 */

public class YTCalllOutActivity extends CallOutgoingActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListener = new LinphoneCoreListenerBase(){
            @Override
            public void callState(LinphoneCore lc, LinphoneCall call, LinphoneCall.State state, String message) {
                if (call == mCall && LinphoneCall.State.Connected == state) {
                    if (!YTLinphoneActivity.isInstanciated()) {
                        return;
                    }
                    YTLinphoneActivity.instance().startIncallActivity(mCall);
                    finish();
                    return;
                } else if (state == LinphoneCall.State.Error) {
                    // Convert LinphoneCore message for internalization
                    if (message != null && call.getErrorInfo().getReason() == Reason.Declined) {
                        displayCustomToast(getString(org.linphone.R.string.error_call_declined), Toast.LENGTH_SHORT);
                    } else if (message != null && call.getErrorInfo().getReason() == Reason.NotFound) {
                        displayCustomToast(getString(org.linphone.R.string.error_user_not_found), Toast.LENGTH_SHORT);
                    } else if (message != null && call.getErrorInfo().getReason() == Reason.Media) {
                        displayCustomToast(getString(org.linphone.R.string.error_incompatible_media), Toast.LENGTH_SHORT);
                    } else if (message != null && call.getErrorInfo().getReason() == Reason.Busy) {
                        displayCustomToast(getString(org.linphone.R.string.error_user_busy), Toast.LENGTH_SHORT);
                    } else if (message != null) {
                        displayCustomToast(getString(org.linphone.R.string.error_unknown) + " - " + message, Toast.LENGTH_SHORT);
                    }
                }

                if (LinphoneManager.getLc().getCallsNb() == 0) {
                    finish();
                    return;
                }
            }
        };
    }
}
